/**   
 * @Title: AbsInferiorityCommunicater.java 
 * @Package com.esun.cheetah.network
 * @Description: 通讯相关
 * @author 程相亭 <br/>
 * @date 2012-03-15 下午05:07:33  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.cheetah.network;

import java.util.HashMap;

import org.json.JSONException;

import android.os.Handler;
import android.os.Message;

import com.esun.cheetah.config.Config;
import com.esun.util.log.EDebug;

/**
 * @ClassName: AbsInferiorityCommunicater
 * @Description:通讯相关类，封装线程及通讯后操作
 * @author 程相亭 <br/>
 * @date 2012-03-15 下午05:07:33
 */
public abstract class AbsInferiorityCommunicater extends AbsServerCommunicater {

	protected Handler handlerFromAct;

	public AbsInferiorityCommunicater(Handler handler) {
		this.handlerFromAct = handler;
	}

	public void startCommunicate() {

		new Thread() {

			@Override
			public void run() {
				super.run();
				communicateWithServer();
			}
		}.start();

	}

	public void communicateWithServer() {

 		HashMap<String, String> hmJson = comHashMap();
		super.isGetServerResponseSuccess(hmJson);
	}

	@Override
	public void parseJson(String serverRes) throws JSONException {
		EDebug.o("------------------------------------->解析json内容开始");
		getJsonContent(serverRes);
	}

	@Override
	public void getServerResponseFail(String serverRes) throws JSONException {

		Message msg = handlerFromAct.obtainMessage(Config.COMMUNICATER_FAIL);
		handlerFromAct.sendMessage(msg);
		EDebug.o("------------------------------------->获取服务器信息失败");
	}

	@Override
	public void getServerResponseSuccess() {
		Message msg = handlerFromAct.obtainMessage(Config.COMMUNICATER_SUCCESS);
		handlerFromAct.sendMessage(msg);
		EDebug.o("------------------------------------->获取服务器信息成功");
	}

	@Override
	public void getServerResponseException() {

		Message msg = handlerFromAct
				.obtainMessage(Config.COMMUNICATER_EXCEPTION);
		handlerFromAct.sendMessage(msg);
		EDebug.o("------------------------------------->获取服务器信息发生异常");
	}

	/**
	 * @throws
	 * @Title: comHashMap
	 * @Description: 组装HashMap参数
	 * @param
	 * @return HashMap 组装好的HashMap
	 * @throws
	 */
	abstract public HashMap<String, String> comHashMap();

	/**
	 * @throws JSONException
	 * @throws
	 * @Title: getJsonContent
	 * @Description: 获取json内容
	 * @param
	 * @return Object 获取到封装好的json内容对象
	 * @throws
	 */
	abstract public void getJsonContent(String serverRes) throws JSONException;

}
